package net.zhengxinyun.performance.service.report;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.Util.DateUtils;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.entity.CountryUnitIndicatorsEntity;
import net.zhengxinyun.performance.entity.IndexReport;
import net.zhengxinyun.performance.entity.TownIndicatorsEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.CountryIndicatorsFilter;
import net.zhengxinyun.performance.filter.CountryUnitIndicatorsFilter;
import net.zhengxinyun.performance.filter.IndexReportFilter;
import net.zhengxinyun.performance.filter.TownIndicatorsFilter;
import net.zhengxinyun.performance.mapper.Indicators.CountryIndicatorsEntityMapper;
import net.zhengxinyun.performance.mapper.Indicators.CountryUnitIndicatorsEntityMapper;
import net.zhengxinyun.performance.mapper.Indicators.TownIndicatorsEntityMapper;
import net.zhengxinyun.performance.mapper.Indicators.TownScoreEntityMapper;
import net.zhengxinyun.performance.mapper.report.IndexReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class IndexReportServiceImpl implements IndexReportService{

    @Autowired
    private IndexReportMapper indexReportMapper;

    @Autowired
    private TownIndicatorsEntityMapper townIndicatorsEntityMapper;

    @Autowired
    private TownScoreEntityMapper townScoreEntityMapper;

    @Autowired
    private CountryIndicatorsEntityMapper countryIndicatorsEntityMapper;

    @Autowired
    private CountryUnitIndicatorsEntityMapper countryUnitIndicatorsEntityMapper;

    /**
     * 保存记录
     * @param
     * @return
     */
    @Override
    public int saveReport(UserEntity userEntity,int indexClass,Long indexId,String datetime) {
        IndexReportFilter reportfiter  = new IndexReportFilter();
        reportfiter.setIndexClass(indexClass);
        reportfiter.setIndexDate(datetime);
        reportfiter.setIndexId(indexId);
        List<IndexReportFilter> indexReportFilters = indexReportMapper.query(reportfiter);
        if(ObjectUtils.isNotBlank(indexReportFilters))
        {
            for(IndexReportFilter item:indexReportFilters)
            {
                indexReportMapper.deleteReport(item.getId());
            }
        }

        IndexReport  report = new IndexReport();
        report.setIndexClass(indexClass);
        report.setIndexWriter(userEntity.getId().toString());
        report.setIndexDate( datetime  );
        report.setIndexTime( DateUtils.getCurrentDateTime());
        report.setIndexId(indexId);
        report.setIndexStatue("1");
        report.setIndexStatue("1");
        int saveCode = indexReportMapper.saveReport(report);
        return saveCode;
    }

    /**
     * 县对乡镇
     * @param filter
     * @return
     */
    @Override
    public  List< HashMap<String, Object>  > queryReport(TownIndicatorsFilter filter) {
        List< HashMap<String, Object>  > listReport = new ArrayList<>();

        filter.setIndexType("二级指标");
        /**
         * 所有二级指标
         */
       // filter.setDateTime(DateUtils.getCurrentYear());
        List<TownIndicatorsFilter> listIndic = townIndicatorsEntityMapper.selectTownIndex(filter);
        TownIndicatorsEntity entity;

        for (TownIndicatorsFilter item : listIndic) {
            HashMap<String, Object> indexMap = new LinkedHashMap<>();
            indexMap.put("id", item.getId());
            indexMap.put("indexType", item.getIndexType());
            indexMap.put("leadUnitName", item.getLeadUnitName());
            indexMap.put("indexName", item.getIndexName());
            indexMap.put("respUnitName", item.getRespUnitName());
            indexMap.put("leadUnit", item.getLeadUnit());
            indexMap.put("responsibilityUnit", item.getRespUnitName());
            indexMap.put("direction", item.getDirection());
            indexMap.put("monthTime", item.getMonthTime());
            indexMap.put("reason", item.getReason());
            indexMap.put("mainUnit", item.getMainUnit());

            IndexReportFilter reportFilter = new IndexReportFilter();
            reportFilter.setIndexId(item.getId());
            reportFilter.setIndexClass(filter.getIndexClass());
            List<IndexReportFilter> reportList = indexReportMapper.selectReport(reportFilter);
            List<HashMap<String,Object> > reportInfoMap = new ArrayList<>();
            if (StringUtils.isNotBlank(item.getSuperiorIndexId())) {
                entity = townIndicatorsEntityMapper.selectByPrimaryKey(Long.valueOf(item.getSuperiorIndexId()));
                if (entity != null) {
                    indexMap.put("superiorIndexId", entity.getIndexName());
                }
            }
            for (IndexReportFilter report:reportList)
            {


                indexMap.put("month"+report.getIndexDate().replace(DateUtils.getCurrentYear(),""), report.getIndexDate() );
                HashMap<String,Object> monthValue = new HashMap<>();
                monthValue.put("userName",report.getUserName());
                monthValue.put("dateTime",report.getIndextime());
                monthValue.put("indexDate",report.getIndexDate());
                reportInfoMap.add(monthValue);
            }

            indexMap.put("reportList",reportInfoMap);

        listReport.add(indexMap);
        }


        return listReport;
    }

    //是对先
    @Override
    public List<HashMap<String, Object>> queryCityReport(TownIndicatorsFilter filter) {
        CountryIndicatorsFilter cityfilter = new CountryIndicatorsFilter();
        List< HashMap<String, Object>  > listReport = new ArrayList<>();
        cityfilter.setIndexType("二级指标");

        if(StringUtils.isBlank(filter.getDateTime()))
        {
            cityfilter.setDateTime(DateUtils.getCurrentYear());
        }
        List<CountryIndicatorsFilter> query = countryIndicatorsEntityMapper.query(cityfilter);

        List<HashMap<String,Object> > reportInfoMap = new ArrayList<>();
        for ( CountryIndicatorsFilter item: query)
        {
            HashMap<String, Object> indexMap = new LinkedHashMap<>();
            indexMap.put("id", item.getId());
            indexMap.put("indexType", item.getIndexType());
            indexMap.put("leadUnitName", item.getLeadUnitName());
            indexMap.put("indexName", item.getIndexName());
            indexMap.put("respUnitName", item.getRespUnitName());
            indexMap.put("leadUnit", item.getLeadUnit());
            indexMap.put("responsibilityUnit", item.getRespUnitName());
            indexMap.put("direction", item.getDirection());
            indexMap.put("monthTime", item.getMonthTime());
            indexMap.put("reason", item.getReason());
            indexMap.put("mainUnit", item.getMainUnit());

            IndexReportFilter reportFilter = new IndexReportFilter();
            reportFilter.setIndexId(item.getId());
            reportFilter.setIndexClass(filter.getIndexClass());
            List<IndexReportFilter> reportList = indexReportMapper.selectReport(reportFilter);

            for (IndexReportFilter report:reportList)
            {
                HashMap<String,Object> infos = new HashMap<>();
                infos.put("dateTime",report.getIndextime());
                infos.put("userName",report.getUserName());
                infos.put("indexDate",report.getIndexDate());
                reportInfoMap.add(infos);
                indexMap.put("month"+report.getIndexDate().replace(DateUtils.getCurrentYear(),""), report.getIndexDate() );
                indexMap.put("reportList",reportInfoMap);
            }

            listReport.add(indexMap);
        }


        return listReport;
    }

    //县直单位
    @Override
    public List<HashMap<String, Object>> queryDepartReport(TownIndicatorsFilter filter) {
        CountryUnitIndicatorsFilter departfilter = new CountryUnitIndicatorsFilter();
        List< HashMap<String, Object>  > listReport = new ArrayList<>();

        if(StringUtils.isBlank(filter.getDateTime()))
        {
            departfilter.setDateTime(DateUtils.getCurrentYear());
        }
        List<CountryUnitIndicatorsEntity> query = countryUnitIndicatorsEntityMapper.query(departfilter);
        for (CountryUnitIndicatorsEntity item: query)
        {
            HashMap<String, Object> indexMap = new LinkedHashMap<>();
            indexMap.put("id", item.getId());
            indexMap.put("indexType", item.getIndexType());
            indexMap.put("leadUnitName", item.getLeadUnitName());
            indexMap.put("indexName", item.getIndexName());
            indexMap.put("leadUnit", item.getLeadUnit());
            indexMap.put("direction", item.getDirection());
            indexMap.put("monthTime", item.getMonthTime());
            indexMap.put("mainUnit", item.getMainUnit());

            IndexReportFilter reportFilter = new IndexReportFilter();
            reportFilter.setIndexId(item.getId());
            reportFilter.setIndexClass(filter.getIndexClass());
            List<IndexReportFilter> reportList = indexReportMapper.selectReport(reportFilter);
            List<HashMap<String,Object> > reportInfoMap = new ArrayList<>();
            for (IndexReportFilter report:reportList)
            {

                indexMap.put("month"+report.getIndexDate().replace(DateUtils.getCurrentYear(),""), report.getIndexDate() );
                HashMap<String,Object> monthValue = new HashMap<>();
                monthValue.put("dateTime",report.getIndextime());
                monthValue.put("userName",report.getUserName());
                monthValue.put("indexDate",report.getIndexDate());
                reportInfoMap.add(monthValue);

                indexMap.put("reportList",reportInfoMap);
            }
            listReport.add(indexMap);
        }

        return listReport;
    }
}
